package ezonsport.android.data;

import java.sql.SQLException;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

import ezonsport.android.R;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * 
 * @author Administrator
 * 
 */
public class DataHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "ezonsport.db";
	private static final int DATABASE_VERSION = 16;

	public DataHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION,
				R.raw.ormlite_config);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.d("DataHelper", "create database");
			for (int i = 0; i < DatabaseConfigUtil.classes.length; i++) {
				TableUtils.createTable(connectionSource,
						DatabaseConfigUtil.classes[i]);
			}
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "创建数据库失败！");
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int arg2, int arg3) {
		try {
			Log.d("DataHelper", "Update Database");
			for (int i = 0; i < DatabaseConfigUtil.classes.length; i++) {
				TableUtils.dropTable(connectionSource,
						DatabaseConfigUtil.classes[i], true);
			}

			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "更新数据库失败");
			e.printStackTrace();
		}
	}

	@Override
	public void onOpen(SQLiteDatabase db) {

		super.onOpen(db);
		Log.d("DataHelper", "database is opened");
	}

	@Override
	public void close() {
		super.close();
		Log.d("DataHelper", "database is closed");

	}

}